import 'package:flutter/material.dart';

class app4 extends StatefulWidget {
  app4({super.key, required this.name});
  String name;

  //组件创建时调用
  @override
  State<app4> createState() {
    print('组件创建时调用');
    return _app4State();
  }
}

class _app4State extends State<app4> {
  String name = '';
  num a = 0;
  //组件初始化时触发,一般用于：对组件变量赋值,网络请求
  @override
  void initState() {
    print('组件初始化');
    super.initState();
    name = widget.name;
    a = 1;
  }

  //触发一次
  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    print('组件依赖发生改变时触发');
  }

  @override
  Widget build(BuildContext context) {
    print('组件构建');
    return Scaffold(
      appBar: AppBar(
        title: Text('组件生命周期'),
        centerTitle: true,
        backgroundColor: Colors.blue,
      ),
      body: Center(
        child: Text(
          '组件生命周期${name + a.toString()}',
          style: TextStyle(fontSize: 20, color: Colors.red),
        ),
      ),
      //按钮
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          setState(() {
            a += 1;
          });
        },
        child: Icon(Icons.add),
      ),
    );
  }
}
